﻿using System;
using System.Collections;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Windows.Forms;

namespace TooltracePC
{
    /// <summary> 
    /// SaveAsYq7 的摘要说明。 
    /// </summary> 
    public class SaveAsDbf
    {
        public SaveAsDbf()
        {
            // 
            // TODO: 在此处添加构造函数逻辑 
            // 
        }
        private string _ErrInfo = "";
        public string ErrInfo
        {
            get { return _ErrInfo; }
        }
        //filename实际上是一个目录 
        public bool Create(string filename, string sql)
        {
            bool r = false;
            string outconnstring = string.Format("Provider = Microsoft.Jet.OLEDB.4.0 ;Data Source ={0};Extended Properties=dBASE IV;", filename);
            OleDbConnection outConn = new OleDbConnection(outconnstring);
            OleDbCommand dc = outConn.CreateCommand();

            try
            {
                outConn.Open();

                dc.CommandType = CommandType.Text;
                dc.CommandText = sql;
                dc.ExecuteNonQuery();

                r = true;
            }
            catch (Exception c)
            {
                _ErrInfo = c.Message;
                MessageBox.Show(_ErrInfo, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                dc.Dispose();

                if (outConn.State == System.Data.ConnectionState.Open)
                    outConn.Close();
                outConn.Dispose();
            }
            return r;
        }
        //filename实际上是一个目录 
        //RecordInfo类包含自动编号,名称,工资3个属性 
        public bool Save(string filename, string sql)
        {
            bool r = false;
            string outconnstring = string.Format("Provider = Microsoft.Jet.OLEDB.4.0 ;Data Source ={0};Extended Properties=dBASE IV;", filename);
            OleDbConnection outConn = new OleDbConnection(outconnstring);
            OleDbCommand dc = outConn.CreateCommand();

            //string tmp = "insert into table1(自动编号,名称,工资) values({0},'{1}',{2})";
            _ErrInfo = "";
            try
            {
                outConn.Open();

                dc.CommandText = sql;

                dc.ExecuteNonQuery();
            }
            catch (Exception err)
            {
                _ErrInfo = err.Message;
            }
            finally
            {
                if (outConn != null)
                    outConn.Close();

                dc.Dispose();
                outConn.Dispose();
            }
            return r;
        }
    }
}